package com.baizhi.springboot.service.impl;

import com.baizhi.springboot.dao.LessonDao;
import com.baizhi.springboot.entity.Counter;
import com.baizhi.springboot.entity.Lesson;
import com.baizhi.springboot.service.LessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Transactional
@Service
public class LessonServiceImpl implements LessonService {
    @Autowired
    private LessonDao lessonDao;
    @Override
    @Transactional(readOnly = true)
    public Map selectLessonByPage(Integer page, Integer limit, String name) {
        Integer offset=(page-1)*limit;
        List<Lesson> lessons = lessonDao.selectLessonByPage(offset, limit,name);
        Integer count = lessonDao.selectCount(name);
        /* "code": 0,
          "msg": "",
          "count": 1000,
          "data": [{}, {}]*/
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", lessons);
        return map;
    }

    @Override
    public void insertLesson(Lesson lesson) {
        lessonDao.insertLesson(lesson);
    }

    @Override
    public void deleteLesson(Integer id) {
        lessonDao.deleteLesson(id);
    }

    @Override
    public Map selectCounterByPage(Integer page, Integer limit, Integer lessonId) {
        Integer offset=(page-1)*limit;
        List<Counter> counters = lessonDao.selectCounterByPage(offset, limit,lessonId);
        Integer count = lessonDao.selectCounterCount(lessonId);
        /* "code": 0,
          "msg": "",
          "count": 1000,
          "data": [{}, {}]*/
        HashMap<Object, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", counters);
        return map;
    }
}
